<?php
class Hydrum_Application_Resource_Frontcontroller extends Zend_Application_Resource_Frontcontroller
{
    /**
     * Initialize Front Controller
     *
     * @return Zend_Controller_Front
     */
    public function init()
    {
        $front = $this->getFrontController();

        $dispatcher = new Hydrum_Controller_Dispatcher_Standard();
        
        $front->setDispatcher($dispatcher);
        
        $this->addControllerDirectory();

        foreach ($this->getOptions() as $key => $value) {
            switch (strtolower($key)) {
                    
                case 'defaultcontrollername':
                    $front->setDefaultControllerName($value);
                    break;

                case 'defaultaction':
                    $front->setDefaultAction($value);
                    break;

                case 'baseurl':
                    if (!empty($value)) {
                        $front->setBaseUrl($value);
                    }
                    break;

                case 'params':
                    $front->setParams($value);
                    break;

                case 'plugins':
                    foreach ((array) $value as $pluginClass) {
                        $stackIndex = null;
                        if(is_array($pluginClass)) {
                            $pluginClass = array_change_key_case($pluginClass, CASE_LOWER);
                            if(isset($pluginClass['class']))
                            {
                                if(isset($pluginClass['stackindex'])) {
                                    $stackIndex = $pluginClass['stackindex'];
                                }

                                $pluginClass = $pluginClass['class'];
                            }
                        }

                        $plugin = new $pluginClass();
                        $front->registerPlugin($plugin, $stackIndex);
                    }
                    break;

                case 'returnresponse':
                    $front->returnResponse((bool) $value);
                    break;

                case 'throwexceptions':
                    $front->throwExceptions((bool) $value);
                    break;

                case 'actionhelperpaths':
                    if (is_array($value)) {
                        foreach ($value as $helperPrefix => $helperPath) {
                            Zend_Controller_Action_HelperBroker::addPath($helperPath, $helperPrefix);
                        }
                    }
                    break;

                default:
                    $front->setParam($key, $value);
                    break;
            }
        }

        if (null !== ($bootstrap = $this->getBootstrap())) {
            $this->getBootstrap()->frontController = $front;
        }

        return $front;
    }

    protected function addControllerDirectory()
    {   
        $front = $this->getFrontController();
                
        $applications = $this->getBootstrap()->getOption('applications');
        
        foreach ($applications as $code => $path)
        {
            try{
                $dir = new DirectoryIterator($path . DS . 'Module');
            } catch(Exception $e) {
                continue;
            }
                 
            foreach ($dir as $file) 
            {
                if ($file->isDot() || !$file->isDir()) 
                {
                    continue;
                }

                $module = $file->getFilename();

                // Don't use SCCS directories as modules
                if (preg_match('/^[^a-z]/i', $module) || ('CVS' == $module)) 
                {
                    continue;
                }                
                
                $controllerDir = $file->getPathname() . DS . 'Controller';
                 
                $front->addControllerDirectory($controllerDir, $module);
            }
        }
        
        return $this;        
    }
}
